package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import Models.SalesOrderView;

public class SalesOrderViewPresenter extends AbstractPresenter<SalesOrderView> {
	final String SINGLEITEM = "salesOrderView";
	final String MULTIITEM = "salesOrderViews";
	
	public ArrayList<SalesOrderView> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public SalesOrderView tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public SalesOrderView createItemFromJSONObject(JSONObject jsonObject){
		SalesOrderView item = new SalesOrderView();
		try{
			item.setExported(jsonObject.getBoolean("exported"));
			item.setOrderDate(timeFormat.parse(jsonObject.getString("sODate")));
			item.setOrderId(jsonObject.getInt("sOId"));
			
			CustomerViewPresenter presenter = new CustomerViewPresenter();
			JSONObject jsonGroup = jsonObject.getJSONObject("sOCust");
			item.setCustomerId(presenter.createItemFromJSONObject(jsonGroup));
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(SalesOrderView item){
		try{
			JSONObject json = new JSONObject();
			json.put("exported", item.isExported());
			json.put("sODate", getDate(item.getOrderDate()));
			json.put("sOId", item.getOrderId());

			CustomerViewPresenter presenter = new CustomerViewPresenter();
			json.put("sOCust", presenter.createJSON(item.getCustomerId()));
			
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public SalesOrderView getEmptyEntity(){
		SalesOrderView entity = new SalesOrderView();
		return entity;
	}
}
